package com.congee02.aop.proxy.dynamic;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author congee(congee02 @ 163.com)
 * @date 1/26/2023 11:50 AM
 */
public class Execution implements Executable {

    private final Object target;
    private final Method method;
    private final Object[] args;

    public Execution(Object target, Method method, Object[] args) {
        this.target = target;
        this.method = method;
        this.args = args;
    }

    public Execution(Object target, String methodName, Class<?>[] parameterTypes, Object[] args) throws NoSuchMethodException {
        this.target = target;
        this.args = args;
        this.method = target.getClass().getMethod(methodName, parameterTypes);
    }

    @Override
    public Object execute() {
        Object retValue = null;
        try {
            retValue = method.invoke(target, args);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return retValue;
    }
}
